﻿open SVMLibrary
open SVM
open InstructionParser


//run the program on the SVM -- call your functions in here.
let run_svm (program : string list) : string =
  let mem =
    [for i = 1 to 100 do
        yield 0.0]

  let r1 = 0.0
  let r2 = 0.0
  let r3 = 0.0
  let r4 = 0.0

  //The SVM is made of 5 elements
  //1. memory list
  //2. register list
  //3. program counter (marking the index of the instruction to be executed)
  //4. a boolean detecting the end of the program
  //5. a list containing a pair label,instruction index used in jump statements.
  let init_svm = (mem,[r1;r2;r3;r4],0,false,[])
  let (m,r,pc,e,labels) = init_svm
  let (_,_,_,_,preproccessed_labels) = preprocess_labels program init_svm
  let preprocessed_svm = (m,r,pc,e,preproccessed_labels)
  run_program program preprocessed_svm


[<EntryPoint>]
let main argv =
  try
    let parsed_program = parse_file "Comparison.txt"
    let dump = run_svm parsed_program
    System.Console.WriteLine(dump)
    0
  with
    | Failure(msg) ->
        System.Console.WriteLine(msg)
        1
    | :? System.IO.FileNotFoundException as e ->
        System.Console.WriteLine(e.Message)
        1
